<template>
  <div>
    <el-dialog
      title="新增"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
    >
      <el-form ref="form" label-width="80px" :model="user" :rules="rules">
        <el-form-item label="用户账号" prop="accountNo">
          <el-input v-model="user.accountNo"></el-input>
        </el-form-item>
        <el-form-item label="用户名称" prop="realName">
          <el-input v-model="user.realName"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="user.remark"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="onSubmit"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/user";
export default {
  data() {
    return {
      user: {
        accountNo: "",
        realName: "",
        remark: "",
        createUser: "",
        updateUser: "",
      },
      rules: {
        accountNo: [
          { required: true, message: "用户账号不能为空", trigger: "blur" },
        ],
        realName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
        ],
      },
      loading: false,
      dialogFormVisible: false,
    };
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          api
            .add(this.user)
            .then(() => {
              this.loading = false;
              // 通知父端组件提交完成了
              this.$emit("handleAddSuccess");
              this.dialogFormVisible = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },

    close() {
      this.dialogFormVisible = false;
    },

    show() {
      this.dialogFormVisible = true;
      this.user.accountNo = "";
      this.user.realName = "";
      this.user.remark = "";
      this.user.createUser = this.$store.state.user.accountNo;
      this.user.updateUser = this.$store.state.user.accountNo;
    },
  },
};
</script>
